package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.modular.system.entity.DsstoreBillDeliveryVHide;
import cn.stylefeng.guns.sys.modular.system.mapper.DsstoreBillDeliveryVHideMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.DsstoreBillDeliveryVHideParam;
import cn.stylefeng.guns.sys.modular.system.model.result.DsstoreBillDeliveryVHideResult;
import cn.stylefeng.guns.sys.modular.system.service.DsstoreBillDeliveryVHideService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * v_bill_delivery中不显示的数据 服务实现类
 * </p>
 *
 * @author jiangge
 * @since 2021-08-18
 */
@Service
public class DsstoreBillDeliveryVHideServiceImpl extends ServiceImpl<DsstoreBillDeliveryVHideMapper, DsstoreBillDeliveryVHide> implements DsstoreBillDeliveryVHideService {

    @Override
    public ResponseData add(DsstoreBillDeliveryVHideParam param) {
        DsstoreBillDeliveryVHide entity = getEntity(param);
        this.save(entity);
        return ResponseData.success();
    }

    @Override
    public void delete(DsstoreBillDeliveryVHideParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(DsstoreBillDeliveryVHideParam param) {
        DsstoreBillDeliveryVHide oldEntity = getOldEntity(param);
        DsstoreBillDeliveryVHide newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DsstoreBillDeliveryVHideResult findBySpec(DsstoreBillDeliveryVHideParam param) {
        return null;
    }

    @Override
    public List<DsstoreBillDeliveryVHideResult> findListBySpec(DsstoreBillDeliveryVHideParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DsstoreBillDeliveryVHideParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    @Override
    public void addList(List<String> idList) {

        //将没有的插入，已经插入的不要在重复插入
        List<String> yiCunZai = this.baseMapper.selectByIdList(idList);
        if (CollectionUtil.isNotEmpty(yiCunZai)) {
            List<String> twoListDifference = getTwoListDifference(idList, yiCunZai);
            this.baseMapper.addList(twoListDifference);
        } else {
            this.baseMapper.addList(idList);
        }
    }

    @Override
    public void deleteList(List<String> idList) {
        this.baseMapper.deleteList(idList);
    }


    public List<String> getTwoListDifference(List<String> totalList, List<String> containsList) {
        HashMap<String, Boolean> map = new HashMap(0);
        for (int i = 0; i < containsList.size(); i++) {
            map.put(containsList.get(i), true);
        }
        containsList.clear();
        for (int i = 0; i < totalList.size(); i++) {
            if (!map.containsKey(totalList.get(i))) {
                containsList.add(totalList.get(i));
            }
        }
        return containsList;
    }

    private Serializable getKey(DsstoreBillDeliveryVHideParam param) {
        return param.getDeliveryId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private DsstoreBillDeliveryVHide getOldEntity(DsstoreBillDeliveryVHideParam param) {
        return this.getById(getKey(param));
    }

    private DsstoreBillDeliveryVHide getEntity(DsstoreBillDeliveryVHideParam param) {
        DsstoreBillDeliveryVHide entity = new DsstoreBillDeliveryVHide();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
